package net.betou.common.util;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Role;
import net.betou.trade.main.manager.UserMng;
import net.betou.trade.main.manager.agencyorproxy.AdminMng;
import net.betou.trade.main.manager.agencyorproxy.ProxyMng;
import net.betou.trade.main.manager.role.AgencyRoleMng;

import org.apache.commons.lang.StringUtils;

public class TradeUtils{

	/**
	 * 用户KEY
	 */
	public static final String USER_KEY = "_user_key";

	/**
	 * 获得用户
	 * 
	 * @param request
	 * @return
	 */
	public static AgentAndAdmins getAdmin( HttpServletRequest request ) {
		return ( AgentAndAdmins ) request.getAttribute( USER_KEY );
	}

	public static void setAdmin( HttpServletRequest request, AgentAndAdmins admin ) {
		request.setAttribute( USER_KEY, admin );
	}
	
	public static Integer getCurrentAdminSmsNum( AgentAndAdmins admin, AdminMng adminMng ){
		
		// 如果是机构或者代理商，直接返回短信数量
		if( admin.getAgency( ) || admin.getProxy( ) ){
			return admin.getSmsNum( ); 
		}
		
		String topIntermedia = admin.getTopIntermedia( ); // 获取topIntermedia, 从这个里面得到代理商或者机构，倒序
		
		if( admin.getSystem( ) ){
			return 9999;
		}
		
		String [ ] tops = StringUtils.split( topIntermedia );
		
		for( int i = tops.length - 1; i >= 0; i-- ){
			Integer id = DataConvertUtils.getInt( tops[ i ] ); // 获取当前的ID
			AgentAndAdmins ad = adminMng.findById( id );
			if( ad == null ){
				continue;
			}
			if( ad.getAgency( ) || ad.getProxy( ) ){
				return ad.getSmsNum( ); 
			}
		}
		
		return 0;
		
	}
	
	@SuppressWarnings( "unchecked" )
	public static Set< String > getMobiles( UserMng userMng, ProxyMng proxyMng,  AgencyRoleMng roleMng, AgentAndAdmins admin, String smsGroups ) {
		// 查询出当前管理员的用户，并且分组信息要在
		List< String > mobileList = userMng.getMobileList( admin, smsGroups );
		List< String > proxyMobileList = null;

		// 如果是代理商，则要添加代理商的
		if( isProxy( roleMng, admin, smsGroups ) ){
			proxyMobileList = proxyMng.getMobileList( admin );
		}

		return mergerList( mobileList, proxyMobileList );
	}
	
	private static Set< String > mergerList( List< String > ... mobileList ) {

		Set< String > newSet = new HashSet< String >( );

		for ( List< String > list : mobileList ){
			fillSet( list, newSet );
		}

		return newSet;
	}
	
	private static void fillSet( List< String > list, Set< String > set ) {

		if( list != null ){
			for ( String m : list ){
				set.add( m );
			}
		}
	}
	
	public static boolean isProxy( AgencyRoleMng agencyRoleMng, AgentAndAdmins admin, String smsGroups ){
		
		// 不是机构的话，就不发送给代理商
		if(! admin.getAgency( ) ){
			return false;
		}
		
		String [ ] smsGroupArr = StringUtils.split( smsGroups, "," );
		
		if( smsGroupArr == null || smsGroupArr.length == 0 ){
			return false;
		}
		
		Role role = agencyRoleMng.findProxyBySelf( admin );
		
		for( String s : smsGroupArr ){
			if( s.equals( role.getId( ).toString( )  )){
				return true;
			}
		}
		return false;
	}
}
